package com.chentongwei.common.util;

import com.chentongwei.common.constant.ResponseEnum;
import com.chentongwei.common.constant.StatusEnum;
import com.chentongwei.common.exception.BussinessException;
import com.chentongwei.common.exception.WebException;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 通用的异常处理
 *
 * @author TongWei.Chen 2017-9-17 17:00:01
 */
public final class CommonExceptionUtil {

    private CommonExceptionUtil() {}

    /**
     * 检查是否为null
     *
     * @param object：obj
     */
    public static final void nullCheck(final Object object) {
        nullCheck(object, ResponseEnum.NULL);
    }

    /**
     * 检查是否为null
     *
     * @param object：obj
     * @param responseEnum：返回消息
     */
    public static final void nullCheck(final Object object, final ResponseEnum responseEnum) {
        if(null == object) {
            throwBussinessException(responseEnum);
        }
    }

    /**
     * 检查是否为null
     *
     * @param object：obj
     * @param responseEnum：返回消息
     */
    public static final void nullCheckThrowWebEx(final Object object, final ResponseEnum responseEnum) {
        if(null == object) {
            throwWebException(responseEnum);
        }
    }

    /**
     * 异常处理：检查集合是否为空
     *
     * @param list
     */
    public static void listCheck(final List<?> list, final ResponseEnum responseEnum) {
        if (CollectionUtils.isEmpty(list)) {
            throwBussinessException(responseEnum);
        }
    }

    /**
     * 检查是否存在
     *
     * @param count > 0：存在
     */
    public static final void existsCheck(final int count, final ResponseEnum responseEnum) {
        if(count > 0) {
            throwBussinessException(responseEnum);
        }
    }
    
    /**
     * 检查是否成功
     *
     * @param flag：true：成功
     */
    public static final void flagCheck(final boolean flag) {
        if(! flag) {
            throwBussinessException(ResponseEnum.OPERATE_ERROR);
        }
    }

    /**
     * 检查是否成功
     *
     * @param flag：true：成功
     */
    public static final void flagCheck(final boolean flag, final ResponseEnum responseEnum) {
        if(flag) {
            throwBussinessException(responseEnum);
        }
    }

    /**
     * 检查两个值是否相等
     * @param field1：字段值1
     * @param field2：字段值2
     */
    public static final void equalsCheck(final Object field1, final Object field2, final ResponseEnum responseEnum) {
        if(! Objects.equals(field1, field2)) {
            throwBussinessException(responseEnum);
        }
    }

    /**
     * 检查两个值是否相等
     * @param field1：字段值1
     * @param field2：字段值2
     */
    public static final void equalsCheckThrowWebEx(final Object field1, final Object field2, final ResponseEnum responseEnum) {
        if(! Objects.equals(field1, field2)) {
            throwWebException(responseEnum);
        }
    }

    /**
     * 判断字符串是否是null
     *
     * @param field:字符串
     * @param responseEnum：提示语
     */
    public static final void strNullCheckThrowWebEx(final String field, final ResponseEnum responseEnum) {
        if(StringUtils.isEmpty(field) || StringUtils.isBlank(field)) {
            throwWebException(responseEnum);
        }
    }

    /**
     * 判断用户是否激活
     *
     * @param activeStatus：用户状态 0未激活 1已激活
     */
    public static final void userActiveCheck(final Integer activeStatus) {
        if(Objects.equals(activeStatus, StatusEnum.USER_IS_NOT_ACTIVE.getCode())) {
            throwBussinessException(ResponseEnum.USER_NOT_ACTIVE);
        }
    }

    /**
     * 判断用户是否已经激活过
     *
     * @param activeStatus：用户状态 0未激活 1已激活
     */
    public static final void alreadyActiveCheck(final Integer activeStatus) {
        if(Objects.equals(activeStatus, StatusEnum.USER_IS_ACTIVE.getCode())) {
            throwBussinessException(ResponseEnum.USER_ALREADY_ACTIVE);
        }
    }

    /**
     * 判断用户是否禁用
     *
     * @param disabledStatus：是否禁用 0禁用 1未禁用
     */
    public static final void userDisabledCheck(final Integer disabledStatus) {
        if(Objects.equals(disabledStatus, StatusEnum.USER_IS_DISABLED.getCode())) {
            throwBussinessException(ResponseEnum.USER_IS_DISABLED);
        }
    }

    /**
     * 抛出Web异常
     *
     * @param responseEnum 枚举常量
     */
    private static final void throwWebException(final ResponseEnum responseEnum) {
        throw new WebException(responseEnum);
    }

    /**
     * 抛出业务异常
     *
     * @param responseEnum 枚举常量
     */
    private static final void throwBussinessException(final ResponseEnum responseEnum) {
        throw new BussinessException(responseEnum);
    }
}
